//
//  ChangeTelNumberViewController.m
//  JianChaRiBao
//
//  Created by Changxy on 13-7-29.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "ChangeTelNumberViewController.h"
#import "AppDelegate.h"
@interface ChangeTelNumberViewController ()

@end

@implementation ChangeTelNumberViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(modifyResult:) name:KmodifyTel object:nil];
    }
    return self;
}
- (BOOL)checkTel:(NSString *)str{
    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isMatch = [pred evaluateWithObject:str];
    return isMatch;
    
}
-(void)modifyResult:(NSNotification *)notifi{
    NSString * str = [notifi object];
    ABPromptView * abp = [[ABPromptView alloc]initWithTitle:str];
    [abp show];
    [abp release];
    if ([str isEqualToString:@"绑定成功"]) {
        [[UIApplication rootNavController] popToRootViewControllerAnimated:YES];
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    telNumber.delegate = self;
    password.delegate = self;
    shouldSwipeBack = YES;
    
}
- (IBAction)backButtonClicked:(id)sender {
    [password resignFirstResponder];
    [telNumber resignFirstResponder];
    [[UIApplication rootNavController]popViewControllerAnimated:YES];
}
- (IBAction)submitButtonClicked:(id)sender {
    [password resignFirstResponder];
    [telNumber resignFirstResponder];
    if (telNumber.text && password.text) {
        if ([self checkTel:telNumber.text]) {
            [[AuthorManager defaultManager] modifyTelNumber:telNumber.text password:password.text];
        }else{
            ABPromptView * abp = [[ABPromptView alloc]initWithTitle:@"手机号码格式错误"];
            [abp show];
            [abp release];
        }
    }else{
        ABPromptView * abp = [[ABPromptView alloc]initWithTitle:@"请填写完整资料"];
        [abp show];
        [abp release];
    }
    
    
    
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [telNumber resignFirstResponder];
    [password resignFirstResponder];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [telNumber release];
    [password release];
    [super dealloc];
}
- (void)viewDidUnload {
    [telNumber release];
    telNumber = nil;
    [password release];
    password = nil;
    [super viewDidUnload];
}
@end
